package org.wanda.pv.service.excel.impl;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Service;
import org.wanda.pv.model.excel.ExcelInfo;
import org.wanda.pv.model.excel.ExcelMark;
import org.wanda.pv.model.excel.ExcelPart;
import org.wanda.pv.model.excel.ExcelPoint;
import org.wanda.pv.service.excel.ExcelExportService;

import java.io.*;
import java.util.List;

/**
 * @author lxy
 * Created by 2019/1/9 15:19
 */
@Service
public class ExcelExportServiceImpl implements ExcelExportService {

	@Override
	public void projectCheckExcelExport(ExcelInfo excelInfo) {
		try {
			OutputStream outputStream = new FileOutputStream("C:\\Users\\lxy\\Desktop\\testExcelcopyF1.xlsx");
			InputStream inputStream = new FileInputStream("C:\\Users\\lxy\\Desktop\\testExcel.xlsx");
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
			XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
			cellStyle.setWrapText(true);
			XSSFWorkbook xssfWorkbook1 = xssfWorkbook;
			XSSFSheet sheet =  xssfWorkbook1.getSheetAt(7);
			XSSFRow row = sheet.getRow(0);
			XSSFCell cell = row.getCell(0);
			cell.setCellValue(excelInfo.getExcelName());
			cell.setCellStyle(cellStyle);
			/*java.lang.System.out.println("ExcelName : " + cell.getStringCellValue());
*/
			List<ExcelPart> excelParts = excelInfo.getExcelParts();
			// 用来记录row的位置
			int lastRowNum = sheet.getLastRowNum();
			int lastPointRowNum = sheet.getLastRowNum();
			/*java.lang.System.out.println("***********" + sheet.getLastRowNum());*/
			// 这一步是循环部位分项 ，
			// ExcelPart excelPart : excelParts
			for (int i =0 ; i < excelParts.size() ; i ++){
				//  部位分项的合并单元格等于 标注的总格数
				int partSize = 0;
				int pointSize = excelParts.get(i).getPointSize();
				List<ExcelPoint> points = null ;
				if (pointSize == 0) {
					pointSize = ++ pointSize;
				} else {
					points = excelParts.get(i).getPoints();
					for (ExcelPoint excelPoint : points){
						// 如果标注是0 ， 就要计算要点的数量
						int markSize = 0;
						// 判断 如果只有一个标注，就不 + 1
						if ( excelPoint.getMarkSize() == 0 ) {
							markSize = markSize + 1;
						} else {
							markSize = excelPoint.getMarkSize();
						}
						partSize = partSize + markSize;
					}
				}
				/*java.lang.System.out.println( excelParts.get(i).getName() + "      当前部件分项占用的row : " + partSize);
				java.lang.System.out.println("当前部件列合并起始 Row ： "+ lastRowNum + "结束： "+ (lastRowNum  + partSize - 1)  );*/
				if (lastRowNum < (lastRowNum  + partSize - 1)) {
					sheet.addMergedRegion(new CellRangeAddress(lastRowNum  ,lastRowNum  + partSize - 1,0,0));
				}
				row = sheet.createRow(lastRowNum);
				cell = row.createCell(0);
				cell.setCellValue(excelParts.get(i).getName());
				int lastRowNum1 = lastRowNum;

				for (int p = 0 ; p < points.size(); p ++ ){
					ExcelPoint excelPoint = points.get(p);
					int markSize = excelPoint.getMarkSize();
					// 	判断如果合并的行数不大于1 就不用合并
					if (markSize > 1) {
					/*	java.lang.System.out.println(" 要点 当前合并起始 Row ： "+ lastPointRowNum + "结束： "+ (lastPointRowNum + markSize  -1) + "标注个数 ： "  + markSize);*/
						sheet.addMergedRegion(new CellRangeAddress(lastPointRowNum,lastPointRowNum + markSize  -1  ,1,1));
						sheet.addMergedRegion(new CellRangeAddress(lastPointRowNum,lastPointRowNum + markSize  -1,2,2));
						sheet.addMergedRegion(new CellRangeAddress(lastPointRowNum,lastPointRowNum + markSize  -1,3,3));
					}
				/*	java.lang.System.out.println(excelPoint.getSerialNumber() + " 当前输入行 ： "+ lastRowNum1   );*/
					XSSFRow  row1 = sheet.getRow(lastRowNum1)==null?sheet.createRow(lastRowNum1):sheet.getRow(lastRowNum1);
					cell = row1 .createCell(1);
					cell.setCellValue(excelPoint.getSerialNumber());
					cell = row1 . createCell(2);
					cell.setCellValue(excelPoint.getContent());
					cell = row1 .createCell(3);
					cell.setCellValue(excelPoint.getPointClass());
					List<ExcelMark> marks = excelPoint.getMarks();
					for (int o =0 ; o < marks.size() ; o ++ ){
						/*java.lang.System.out.println("标注 " + marks.get(o).getMarkContent() + " 当前输入行 ： " + (lastRowNum1 + o) );*/
						row1 = sheet.getRow(lastRowNum1 + o )== null ?  sheet.createRow(lastRowNum1 + o ):sheet.getRow(lastRowNum1 + o);
						cell = row1 .createCell(4);
						cell.setCellValue(marks.get(o).getMarkContent());
						cell.setCellStyle(cellStyle);
						cell = row1 .createCell(5);
						cell.setCellValue(marks.get(o).getIsOptimization());
						cell.setCellStyle(cellStyle);
						if (marks.get(o).getDeExaminesContent() != null){
							cell = row1 .createCell(6);
							cell.setCellValue(marks.get(o).getDeExaminesContent());
							cell.setCellStyle(cellStyle);
							cell = row1 .createCell(7);
							cell.setCellValue(marks.get(o).getWdExaminesContent());
							cell.setCellStyle(cellStyle);
							cell = row1 .createCell(8);
							cell.setCellValue(marks.get(o).getPvExaminesContent());
							cell.setCellStyle(cellStyle);
						}
					}
					/*
					 *  判断 ， 如果标注数量为0 ，要点行和标注行都要 + 1
					 *  反之， 如果标注有数量，就计算标注的数量
					 *  这是记录下一行的其实row 的 逻辑
					 * */
					if (  markSize == 0) {
						lastRowNum1 = lastRowNum1 + 1 ;
						lastPointRowNum = lastPointRowNum + 1 ;
					} else {
						lastRowNum1 = lastRowNum1 + markSize;
						lastPointRowNum = lastPointRowNum + markSize ;
					}
					cell.setCellStyle(cellStyle);
				}

				lastRowNum = lastRowNum + partSize  ;
			}
			xssfWorkbook1.write(outputStream);
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



}
